package com.achobeta.recruitment.service.impl.student;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.config.AppConfig;
import com.achobeta.recruitment.dao.student.StudentInfoMapper;
import com.achobeta.recruitment.model.StudentInfoWithBLOBs;
import com.achobeta.recruitment.model.vo.StudentInfoVO;
import com.achobeta.recruitment.service.manager.ManageRemarksService;
import com.achobeta.recruitment.service.student.StudentInfoService;
import com.achobeta.recruitment.service.student.StudentService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.MediaUtils;
import com.achobeta.recruitment.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-30
 * Time: 16:41
 */
@Service
@Slf4j
public class StudentInfoServiceImpl implements StudentInfoService {

    @Value("${redirect.domain}")
    private String redirectDomain;


    @Autowired
    private StudentInfoMapper studentInfoMapper;

    @Autowired
    private ManageRemarksService manageRemarksService;

    @Autowired
    private StudentService studentService;


    @Override
    public void initStudentInfo(String openid) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        // 获得一个学生对象
        StudentInfoWithBLOBs studentInfo = new StudentInfoWithBLOBs();
        // 初始化值（尽可能少减少 mysql 的开销）
        studentInfo.setOpenid(openid);
        studentInfo.setState((byte) 0);
        studentInfo.setWxName("");
        studentInfo.setStudentId("");
        studentInfo.setName("");
        studentInfo.setGender((byte) -1);
        studentInfo.setGrade(0);
        studentInfo.setMajor("");
        studentInfo.setClassNumber((byte) 0);
        studentInfo.setPhone("");
        studentInfo.setReason("");
        studentInfo.setIntroduce("");
        studentInfo.setExperience("");
        studentInfo.setPrize("");
        studentInfo.setPhoto("");
        studentInfo.setRemark("");
        studentInfo.setDeleteState((byte) 0);
        Date date = new Date();//当前时间
        studentInfo.setCreateTime(date);
        studentInfo.setUpdateTime(date);
        // 插入数据库
        int rows = studentInfoMapper.insertSelective(studentInfo);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
        // 成功
        log.info(ResultCode.SUCCESS.toString() + "  openid = " + openid);
    }

    @Override
    public void saveStudentInfo(StudentInfoWithBLOBs studentInfo) {
        // 校验
        CheckUtils.checkNULL(studentInfo);
        String fileName = studentInfo.getPhoto();
        if(StringUtils.hasLength(fileName)) {
            CheckUtils.checkFileExists(fileName);
        }
        // 更新
        int rows = studentInfoMapper.updateByPrimaryKeySelective(studentInfo);
        // 校验
        System.out.println(rows);
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
        // 成功
        log.info(ResultCode.SUCCESS.toString() + "  openid = " + studentInfo.getOpenid());
    }

    @Override
    public StudentInfoWithBLOBs selectStudentInfoByOpenId(String openid, Byte state) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        // 用openid查询信息
        StudentInfoWithBLOBs studentInfo = studentInfoMapper.selectStudentInfoByOpenid(openid, state);
        // 无论如何都返回
        return studentInfo;
    }


    @Override
    public void submitStudentInfo(StudentInfoWithBLOBs studentInfo) {
        // 校验
        CheckUtils.checkNULL(studentInfo, studentInfo.getClassNumber(),
                studentInfo.getGender(), studentInfo.getGrade());
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, studentInfo.getStudentId(),
                studentInfo.getOpenid(), studentInfo.getExperience(), studentInfo.getIntroduce(),
                studentInfo.getPrize(), studentInfo.getReason(), studentInfo.getMajor(),
                studentInfo.getName(), studentInfo.getPhone());
        CheckUtils.checkFileExists(studentInfo.getPhoto());
        // 更新
        studentInfo.setState((byte) 1);
        int rows = studentInfoMapper.insertSelective(studentInfo);
        // 校验
        System.out.println(rows);
        switch (rows) {
            case 1:
                // 成功
                log.info(ResultCode.SUCCESS.toString() + "  openid = " + studentInfo.getOpenid());
                // 成功之后要初始化student和remarks表
                // 初始化 t_manage_remarks表
                Integer remarkId = manageRemarksService.initManageRemarks();
                // 初始化 t_student表
                studentService.initStudent(studentInfo.getOpenid(), remarkId);
                break;
            case 2:
                // 修改成功
                log.info(ResultCode.SUCCESS.toString() + "  openid = " + studentInfo.getOpenid());
                break;
            default:
                CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
                break;
        }
    }

    @Override
    public void loadImage(MultipartFile file, String path) {
        CheckUtils.checkNULL(file, path);
        // 下载
        MediaUtils.loadFile(file, AppConfig.ROOT + path);
        log.info(ResultCode.SUCCESS + "保存成功：" + redirectDomain + "/" + path);
    }

    @Override
    public StudentInfoVO selectDetailStudentInfoByOpenid(String openid) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        // 调用dao层
        StudentInfoVO studentInfoVO = studentInfoMapper.selectDetailStudentInfoByOpenid(openid);
        // 校验
        CheckUtils.checkNULL(studentInfoVO);
        // 成功
        log.info(ResultCode.SUCCESS + "  用户信息被查询，openid: " + openid);
        return studentInfoVO;
    }

    @Override
    public void updateInfo(StudentInfoWithBLOBs studentInfo, Byte stateId) {
        CheckUtils.checkNULL(studentInfo, studentInfo.getClassNumber(),
                studentInfo.getGender(), studentInfo.getGrade());
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, studentInfo.getStudentId(),
                studentInfo.getOpenid(), studentInfo.getExperience(), studentInfo.getIntroduce(),
                studentInfo.getPrize(), studentInfo.getReason(), studentInfo.getMajor(),
                studentInfo.getName(), studentInfo.getPhone());
        CheckUtils.checkFileExists(studentInfo.getPhoto());
        // 1. 修改学生基本信息
        submitStudentInfo(studentInfo);
        // 2. 修改状态
        String openid = studentInfo.getOpenid();
        studentService.updateState(openid, stateId);
        // 成功
        log.info(ResultCode.SUCCESS + " 管理员的修改生效");
    }
}
